package org.shagin.jfin.ui.budget.item.impl;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

import org.shagin.jfin.core.Debug;
import org.shagin.jfin.data.model.Account;
import org.shagin.jfin.ui.UI;
import org.shagin.swing.layout.FormLayoutPanel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The Class AccountView.
 */
@SuppressWarnings("serial")
public class AccountView extends AbstractBudgetItemView {

    private static final Logger log = LoggerFactory
            .getLogger(AccountView.class);

    private FormLayoutPanel pane = new FormLayoutPanel(UI.GAP);
    private JTextField title = new JTextField();
    private JTextField value = new JTextField();

    /**
     * Instantiates a new account view.
     */
    public AccountView() {
        super();
        Debug.traceEnter(log);
        setLayout(new BorderLayout());
        add(pane, BorderLayout.NORTH);
        pane.add("Name:", title);
        pane.wrap();
        pane.add("Value:", value);
        value.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Debug.traceEnter(log, value.getText());
                getAccount().getAmount().setValue(
                        Double.parseDouble(value.getText()));
                getAccount().getBudget().fireAccountChangedEvent(getAccount());
            }
        });

        title.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Debug.traceEnter(log, title.getText());
                getAccount().setId(title.getText());
                getAccount().getBudget().fireAccountChangedEvent(getAccount());
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setBudgetItem(Object budgetItem) {
        Debug.traceEnter(log);
        super.setBudgetItem(budgetItem);
        title.setText(getAccount().getId());
        value.setText("" + getAccount().getAmount().getValue());
    }

    /**
     * Gets the account.
     * 
     * @return the account
     */
    public Account getAccount() {
        return (Account) getBudgetItem();
    }

}
